var myServiceModule = angular.module("MyServiceModule",[]);

myServiceModule.factory("userListService",["$http",function ($http) {
    var doRequest = function (username,path) {
        return $http({
            method:"GET",
            url:"data.json"
        });
    }
    return {
        userList:function (username) {
            return doRequest(username,"userList");
        }
    }
}]);

/**
 * 自定义service规范：
 *  1：名称不要以$开头
 *  2：放在参数最后一位
 */
myServiceModule.controller("ServiceCtrl",["$scope","$timeout","userListService",function($scope,$timeout,userListService){
        var timeout;
        $scope.$watch('username',function(newUserName){
            if(newUserName){
                if(timeout){
                    $timeout.cancel(timeout);
                }
                timeout = $timeout(function () {
                    userListService.userList(newUserName)
                        .success(function (data,status) {
                            $scope.users = data;
                        });
                },350);
            }
        })
    }]
);